import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    // meta: {
    //    auth: true, //登录

    // },
    children: [
      {
        path: '',
        component: () => import('../views/defaultpage.vue'),
        meta: {
          auth: true, //登录

        }
      },
      {
        //客户管理
        path: 'clientman',
        component: () => import('../views/clientman.vue'),
        meta: {
          auth: true, //登录

        },
        children:[
          {
            path:'',
            component: () => import('../components/clientpage/information'),
          },
          {
            path:'information',
            component: () => import('../components/clientpage/information'),
          },
          {
            path:'card',
            component: () => import('../components/clientpage/card'),
          },
          {
            path:'credits',
            component: () => import('../components/clientpage/credits'),
          },
        ]
      },
      {
        //商品基础设置
        path: 'goods',
        name: 'Goods',
        component: () => import('../views/goods.vue'),
        meta: {
          auth: true, //登录

        },
        children:[
          {
            path:'costmng',
            component: () => import('../components/goodsset/CostManagement'),
          },
          {
            path:'newpro',
            component: () => import('../components/goodsset/NewProductCode'),
          },
        ]
      },
      {
        //商品管理
        path: 'goodsman',
        component: () => import('../views/goodsman.vue'),
        meta: {
          auth: true, //登录

        }
      },
      {
        //门店
        path: 'shopDetail',
        component: () => import('../views/shopDetail.vue')
      }, 
      {
        //权限
        path: 'limits',
        component: () => import('../views/limits.vue')
  
      },
       //权限
      // {  path: 'name',
      //   component: () => import('../components/limits-wll.vue')
      // },
     
      {
        //公告
        path: 'notice',
        component: () => import('../views/notice.vue'),
        meta: {
          auth: true, //登录

        }
      },
      {
        //修改密码
        path: 'password',
        component: () => import('../views/password.vue'),
        meta: {
          auth: true, //登录

        }
      },
      {
        //供应商
        path: 'provider',
        component: () => import('../views/provider.vue'),
        meta: {
          auth: true, //登录

        }
      },
      {
        //员工
        path: 'staff',
        component: () => import('../views/staff.vue'),
        // meta: {
        //   auth: true, //登录
        // }
        children:[
          {
            path:'',
            component: () => import('../components/staff/staffview'),
          },
          {
            path:'staffadd',
            component: () => import('../components/staff/staffadd'),
          },
          {
            path:'staffeditor',
            component: () => import('../components/staff/staffeditor'),
          },
          {
            path:'staffview',
            component: () => import('../components/staff/staffview'),
          },
          {
            path:'staffclass',
            component: () => import('../components/staff/staffclass'),
          },
          {
            path:'consumption',
            component: () => import('../components/clientpage/consumption'),
          },
        ]
      },
      {
        //测试接口
        path:'test',
        component: () => import('../views/test.vue'),
      }
      // {
      //   //门店信息
      //   path:'shopDetail',
      //   component: () => import('../views/shopDetail.vue')
      // }
    ]

  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login')
  }
]

// 实例化路由对象
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 路由拦截（是否已经登录）
router.beforeEach((to, from, next) => {
  // 获取 token
  let token = localStorage.getItem('token')

  if (to.meta.auth) { // 该路由需要登录才能访问
    if (token && store.state.user) { // 已经登陆
      next() // 想干嘛干嘛去
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath // 跳转之前，将原本想访问的路径保存在 query
        }
      })
    }
  }  else {

    next() // 该去哪就去哪
  }




    // if (to.meta.auth) {
    //   console.log(111, store.state.user, to.meta.roles)
    //   if (store.state.user) { // 登录
    //   } else {
    //     next({
    //       path: '/login',
    //       query: {
    //         redirect: to.fullPath
    //       }
    //     })
    //   }
    // } else {
    //   next()
    // }
})


export default router